//
//  RooterViewController.swift
//  WeiBo2017
//
//  Created by 王璐 on 2017/4/12.
//  Copyright © 2017年 wl. All rights reserved.
//

import UIKit
import HMRefresh

/// MARK - 根视图控制器 其他控制器的基类
class RooterViewController: UIViewController {
    
    ///公共属性
    
    
    ///tableView
    var tableView : UITableView?
    /// 下拉刷新控件
    var refreshControl:HMRefreshControl?

    var vistorView : VisterView?
    
    override func loadView() {
        
        view = UIView()
        
        //决定控制器的view
        UserCount.sharedUserCount.isLogin ? setupTableView() : setUpVisterView()

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //没有登录成功注册通知
        if UserCount.sharedUserCount.isLogin {
            loadData()
        }else{
            NotificationCenter.default.addObserver(
                self,
                selector: #selector(RooterViewController.loginSuccessed),
                name: NSNotification.Name(rawValue: WLWeiBoLoginSuccessdNotification),
                object: nil)
        }
        
        
    }
    
    //登录成功监听方法
    @objc private func loginSuccessed(){
        
        view = nil
        //注销通知
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: WLWeiBoLoginSuccessdNotification), object: nil)
    }
    
    
    /// 加载数据,真正的加载数据在子类实现
    func loadData() {
        print("RooterVC基类loadData")
    }

}

// MARK: - 访客视图协议VistorViewDelegate
extension RooterViewController : VistorViewDelegate{
    
    func vistorViewDidRegister() {
        print("注册")
    }
    
    func vistorViewDidLogin() {
        print("登录")
        let oauthVC = OAuthViewController()
        let nav = UINavigationController(rootViewController: oauthVC)
        present(nav, animated: true, completion: nil)
    }
       
}

// MARK: - 设置界面
extension RooterViewController{
    
    /// 创建访客视图
    fileprivate func setUpVisterView(){
        
        vistorView = VisterView()
        view.addSubview(vistorView!)
        
        vistorView?.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        vistorView?.delegate = self
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(
            title: "注册",
            style: UIBarButtonItemStyle.plain,
            target: self,
            action: #selector(RooterViewController.vistorViewDidRegister))
        navigationItem.rightBarButtonItem = UIBarButtonItem(
            title: "登录",
            style: UIBarButtonItemStyle.plain,
            target: self,
            action: #selector(RooterViewController.vistorViewDidLogin))
        
    }
    
    /// 创建表格视图tableView
    fileprivate func setupTableView(){
        
        tableView = UITableView(frame: .zero, style: .plain)
        
        view.addSubview(tableView!)
        
        tableView?.snp.makeConstraints({ (make) in
            make.edges.equalTo(view)
        })
        
        tableView?.dataSource = self
        
        tableView?.delegate = self
        
    }
    /// 刷新控件
    func setupRefreshControl(){
        
        guard let tableView = tableView else {
            return
        }
        
        if refreshControl != nil {
             return
        }
        
        refreshControl = HMRefreshControl()
        
        tableView.addSubview(refreshControl!)
        
        //设置下拉刷新视图
        refreshControl?.pulldownView = WLPullDownRefreshView.refreshView()
        refreshControl?.normalString = "下拉起飞"
        refreshControl?.pullingString = "放开起飞"
        refreshControl?.refreshingString = "正在起飞"
        refreshControl?.lastRefreshString = "末次 "
        
        
       // tableView.tableFooterView = pullUpView
        
        refreshControl?.addTarget(self, action: #selector(RooterViewController.loadData), for: .valueChanged)
    }
}

// MARK: - 基类的数据源方法
extension RooterViewController :UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
//    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//       let section = tableView.numberOfSections-1
//       let row = tableView.numberOfRows(inSection: section)-1
//        
//        if section == indexPath.section && row == indexPath.row && !pullUpView.isAnimating{
//          //  print("上拉加载")
//            pullUpView.startAnimating()
//            loadData()
    
//        }
//    }
    
}
